//
//  YBLoveGodNotiCellData.m
//  yibeilive
//
//  Created by 南无本师释迦牟尼佛 on 2022/11/18.
//

#import "YBLoveGodNotiCellData.h"

@implementation YBLoveGodNotiCellData
//重写父类的 getCellData: 方法。用于把 V2TIMMessage 转换成消息列表 Cell 的绘制数据 TUILinkCellData。
+ (TUIMessageCellData *)getCellData:(V2TIMMessage *)message {
    NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];
    YBLoveGodNotiCellData *cellData = [[YBLoveGodNotiCellData alloc] initWithDirection:message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming];
    cellData.innerMessage = message;
    cellData.text = param[@"text"];
    cellData.uuid = param[@"uuid"];
    return cellData;
}

//重写父类的 getDisplayString: 方法。用于把 V2TIMMessage 转换成会话列表 lastMsg 的展示文本信息。会话列表 lastMsg 展示文本指的是当用户停留在会话列表，每个会话 cell 会显示当前会话最后一条消息。
+ (NSString *)getDisplayString:(V2TIMMessage *)message {
    return @"[系统消息]";
}

//根据消息方向（收/发）初始化消息单元 - 除了基本消息的初始化外，还包括根据方向设置方向变量、昵称字体等。 - 同时为子类提供可继承的行为。
- (instancetype)initWithDirection:(TMsgDirection)direction
{
    self = [super initWithDirection:direction];
    if (self) {
        if (direction == MsgDirectionIncoming) {
            self.cellLayout = [TUIMessageCellLayout incommingTextMessageLayout];
        } else {
            self.cellLayout = [TUIMessageCellLayout outgoingTextMessageLayout];
        }
    }
    return self;
}

//重写父类的 contentSize： 方法，用于计算 cellData 内容所占绘制区域的大小。
- (CGSize)contentSize {
    
    float x = 20 + 0.5 + 12*2;
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(TTextMessageCell_Text_Width_Max - x, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName : kSystemFont(15) } context:nil];
    CGSize size = CGSizeMake(ceilf(rect.size.width), ceilf(rect.size.height));

    // 加上气泡边距
    size.height += self.cellLayout.bubbleInsets.top+self.cellLayout.bubbleInsets.bottom;
    size.width += self.cellLayout.bubbleInsets.left+self.cellLayout.bubbleInsets.right + x;
    
    if (self.direction == MsgDirectionIncoming) {
        size.width = MAX(size.width, [TUIBubbleMessageCellData incommingBubble].size.width);
        size.height = MAX(size.height, [TUIBubbleMessageCellData incommingBubble].size.height);
    } else {
        size.width = MAX(size.width, [TUIBubbleMessageCellData outgoingBubble].size.width);
        size.height = MAX(size.height, [TUIBubbleMessageCellData outgoingBubble].size.height);
    }

    return size;
}

@end
